﻿using System;
using System.Collections;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.ANNOUNCEMENT.Components;

namespace ASPNETVN.PORTAL.Modules.ANNOUNCEMENT.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Item.DeleteItem(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            Item item = new Item();
            if (this.ItemID > 0)
            {
                item = Item.Load(this.ItemID);
            }
            item.Title = txtTitle.Text;
            item.Description = Server.HtmlEncode(txtContent.Value);
            item.ImageUrl = DropDownListIcon.SelectedValue;
            item.ModuleID = this.ModuleID;

            if (this.ItemID == 0)
            {
                item.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                item.CreatedDate = DateTime.Now;
                item.Insert();
            }
            else
            {
                item.Update();
            }
            this.ReturnToReferrerPage();
        }

        public static string[] GetImageList()
        {
            ArrayList ret = new ArrayList();
            string path = HttpContext.Current.Server.MapPath("~/Modules/ASPNETVN.PORTAL.Modules.ANNOUNCEMENT/Images/Icons");
            if (Directory.Exists(path))
            {
                string[] files = Directory.GetFiles(path);
                foreach (string s in files.Where(x => x.LastIndexOf(".jpg") >= 0 || x.LastIndexOf(".gif") >= 0 || x.LastIndexOf(".png") >= 0 || x.LastIndexOf(".bmp") >= 0))
                {
                    string file = s.Substring(s.LastIndexOf('\\') + 1);
                    ret.Add(file);
                }
            }
            return (string[])ret.ToArray(typeof(string));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // Icons.
                foreach (string s in GetImageList())
                {
                    DropDownListIcon.Items.Add(new ListItem(s, "~/Modules/ASPNETVN.PORTAL.Modules.ANNOUNCEMENT/Images/Icons/" + s));
                }

                if (this.ItemID > 0)
                {
                    Item item = Item.Load(this.ItemID);
                    if (item != null)
                    {
                        // Tiêu đề
                        txtTitle.Text = item.Title;
                        // Nội dung.
                        txtContent.Value = Server.HtmlDecode(item.Description);
                        try
                        {
                            if (item.ImageUrl.Length > 0)
                            {
                                DropDownListIcon.SelectedValue = item.ImageUrl;
                            }

                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }
    }
}